package com.example.wacoffee_manage.controller;

import com.example.wacoffee_manage.Service.supplyService;
import com.example.wacoffee_manage.common.PageResult;
import com.example.wacoffee_manage.pojo.Supply;
import com.example.wacoffee_manage.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/supplies")
public class supplyController {
    @Autowired
    private supplyService supplyService;

    @GetMapping
    public Result getAllSupplies(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false, name = "name") String name){
        Supply supply=new Supply();
        supply.setPage(currentPage);
        supply.setPageSize(pageSize);
        if(name!=null)supply.setName(name);
        PageResult supplies = supplyService.getAll(supply);
        return Result.success(supplies);
    }

    @GetMapping("/products")
    public Result getProductsBySupplyId(@RequestParam Integer id){
        return Result.success(supplyService.getProductsBySupplyId(id));
    }

    @PutMapping("/addStock")
    public Result addStock(@RequestParam Integer id,@RequestParam Integer productId,@RequestParam Integer stock){
        supplyService.addStock(id,productId,stock);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getSupplyById(@PathVariable Integer id){
        Supply supply=supplyService.getSupplyById(id);
        return Result.success(supply);
    }

    @PutMapping
    public Result update(@RequestBody Supply supply){
        supplyService.update(supply);
        return Result.success();
    }

    @DeleteMapping()
    public Result delete(@RequestParam Integer id){
        supplyService.delete(id);
        return Result.success();
    }
}